package org.lifeaddict.sybill;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
 
public class MyListAdapter extends BaseExpandableListAdapter {
 
	private Context context;
	private ArrayList<HeaderInfo> deptList;
  
	public MyListAdapter(Context context, ArrayList<HeaderInfo> deptList) {
		this.context = context;
		this.deptList = deptList;
	}
  
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<DetailInfo> lvList = deptList.get(groupPosition).getLvList();
		return lvList.get(childPosition);
	}
 
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
 
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View view, ViewGroup parent) {
   
		DetailInfo detailInfo = (DetailInfo) getChild(groupPosition, childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.child_row, null);
		}

		TextView childItem = (TextView) view.findViewById(R.id.childItem);
		TextView childLocation = (TextView) view.findViewById(R.id.childLocation);
		TextView childTime = (TextView) view.findViewById(R.id.childTime);
		TextView childAlarmTime = (TextView) view.findViewById(R.id.childAlarmTime);
		childItem.setText(detailInfo.getName().trim());
		childLocation.setText(detailInfo.getLocation().trim());
		childTime.setText(detailInfo.getTime().trim());
		childAlarmTime.setText(detailInfo.getAlarmTime().trim());
   
		return view;
	}
 
	@Override
	public int getChildrenCount(int groupPosition) {
   
		ArrayList<DetailInfo> lvList = deptList.get(groupPosition).getLvList();
		return lvList.size();
	}
 
	@Override
	public Object getGroup(int groupPosition) {
		return deptList.get(groupPosition);
	}
 
	@Override
	public int getGroupCount() {
		return deptList.size();
	}
 
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}
 
	@Override
	public View getGroupView(int groupPosition, boolean isLastChild, View view,
			ViewGroup parent) {
   
		HeaderInfo headerInfo = (HeaderInfo) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.group_heading, null);
		}
   
		TextView heading = (TextView) view.findViewById(R.id.heading);
		TextView headDate = (TextView) view.findViewById(R.id.headDate);
		heading.setText(headerInfo.getName().trim());
		headDate.setText(headerInfo.getDate().trim());
   
		return view;
	}
 
	@Override
	public boolean hasStableIds() {
		return true;
	}
 
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
